package academic.ui.client.view;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;

import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.BuyAsGiftPresenter;
import academic.ui.client.widgets.forms.BuyAsGiftForm;

public class BuyAsGiftView extends ViewImpl implements BuyAsGiftPresenter.MyView {

	public GenericContainerTag div = new GenericContainerTag("div");
	
	
	private BuyAsGiftForm giftRecipForm = new BuyAsGiftForm();
	private GenericContainerTag addGiftRecipient = new GenericContainerTag("span","submit");
	private GenericClickable addGiftRecipientButton = new GenericClickable("a");
	
	public GenericContainerTag slot = new GenericContainerTag("div");
	public GenericClickable cancel = new GenericClickable("a","cancel");
	private GenericContainerTag buyAsGiftDiv = new GenericContainerTag("div");
	
	private KeyDownHandler submitFormHandler = new KeyDownHandler() {

		@Override
		public void onKeyDown(KeyDownEvent event) {
			if(event.getNativeKeyCode()==KeyCodes.KEY_ENTER) {
				NativeEvent e = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
				DomEvent.fireNativeEvent(e, addGiftRecipientButton);
			}
		}
		
	};
	
	private GenericContainerTag loginBody = new GenericContainerTag("div");
	
	@Inject
	public BuyAsGiftView(CSSConstants css) {
		super();
		div.setStyleName("gift");
		addGiftRecipient.setStyleName("submitButton");
		addGiftRecipientButton.setStyleName("submitButtonLink");
		addGiftRecipientButton.add(addGiftRecipient);
		loginBody.setStyleName("loginBody");

		loginBody.add(buyAsGiftDiv);
		buyAsGiftDiv.clear();		
		//add both forms
		giftRecipForm.reset();
		
		buyAsGiftDiv.add(giftRecipForm);
		buyAsGiftDiv.add(addGiftRecipientButton);
		buyAsGiftDiv.add(cancel);
		giftRecipForm.getEmailKeyDown().addKeyDownHandler(submitFormHandler);
		giftRecipForm.getMessageKeyDown().addKeyDownHandler(submitFormHandler);
		div.add(loginBody);
	}
	
	@Override
	public Widget asWidget() {
		return div;
	}

	@Override
	public void setup() {
		
	}

	/**
	 * Returns false if there are any errors
	 */
	@Override
	public Boolean checkRules() {
		return giftRecipForm.checkRules();
	}
	
	@Override
	public BuyAsGiftForm getForm() {
		return giftRecipForm;
	}

	@Override
	public HasClickHandlers cancel() {
		return cancel;
	}
	
	@Override
	public HasClickHandlers submit() {
		return addGiftRecipientButton;
	}

	@Override
	public native void updateHiddenElems(String email, String message, String senddate) /*-{
		$wnd.$('#checkoutWithGift').html('gift for '+email);
		$wnd.$('input[name="email"]').val(email);
		$wnd.$('input[name="message"]').val(message);
		$wnd.$('input[name="senddate"]').val(senddate);
		$wnd.$('input[name="hasGift"]').val('true');
	}-*/;


}
